package com.zmc.member.infrastructure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmc.common.utils.PageUtils;
import com.zmc.common.utils.token.AuthTokenService;
import com.zmc.member.infrastructure.bean.query.ShoppingCartQuery;
import com.zmc.member.infrastructure.bean.vo.ShoppingCartItemUpdateCountVo;
import com.zmc.member.infrastructure.bean.vo.ShoppingCartSelectedVo;
import com.zmc.member.infrastructure.bean.vo.ShoppingCartVo;
import com.zmc.member.infrastructure.service.ShoppingCartItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zmc.member.infrastructure.dao.ShoppingCartItemDao;
import com.zmc.member.infrastructure.entity.ShoppingCartItemEntity;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author zmc
 */
@Service("shoppingCartItemService")
public class ShoppingCartItemServiceImpl extends ServiceImpl<ShoppingCartItemDao, ShoppingCartItemEntity> implements ShoppingCartItemService {

    @Autowired
    private AuthTokenService authTokenService;

    @Override
    @Transactional
    public void setSelectedStatus(ShoppingCartSelectedVo vo) {
        LambdaUpdateWrapper<ShoppingCartItemEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ShoppingCartItemEntity::getMemberId, authTokenService.getMemberInfo().getMemberId())
                .in(ShoppingCartItemEntity::getSkuId, vo.getSkuIds())
                .set(ShoppingCartItemEntity::getSelected, vo.getSelected());
        update(wrapper);
    }

    @Override
    @Transactional
    public void clearHasSelected() {
        baseMapper.clearHasSelected(authTokenService.getMemberInfo().getMemberId());
    }

    @Override
    public List<ShoppingCartItemEntity> getListBySkuIds(List<String> skuIds) {
        LambdaQueryWrapper<ShoppingCartItemEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCartItemEntity::getMemberId, authTokenService.getMemberInfo().getMemberId())
                .in(ShoppingCartItemEntity::getSkuId, skuIds);
        return list(wrapper);
    }

    public ShoppingCartItemEntity getOneBySkuId(String skuId) {
        LambdaQueryWrapper<ShoppingCartItemEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCartItemEntity::getMemberId, authTokenService.getMemberInfo().getMemberId())
                .eq(ShoppingCartItemEntity::getSkuId, skuId);
        return getOne(wrapper);
    }

    @Override
    @Transactional
    public void saveOrUpdateItemList(List<ShoppingCartItemEntity> items) {
        List<ShoppingCartItemEntity> queryList = getListBySkuIds(items.stream().map(ShoppingCartItemEntity::getSkuId).collect(Collectors.toList()));
        List<ShoppingCartItemEntity> updateList = new ArrayList<>();
        List<ShoppingCartItemEntity> saveList = new ArrayList<>();
        for (ShoppingCartItemEntity entity: items) {
            if (queryList.stream().anyMatch(query -> query.getSkuId().equals(entity.getSkuId()))) {
                updateList.add(entity);
            } else {
                saveList.add(entity);
            }
        }
        saveBatch(saveList);
        baseMapper.batchUpdateItem(authTokenService.getMemberInfo().getMemberId(), updateList);
    }

    @Override
    @Transactional
    public void removeBySkuIds(List<String> skuIds) {
        baseMapper.removeBySkuIds(skuIds, authTokenService.getMemberInfo().getMemberId());
    }

    @Override
    @Transactional
    public void updateShoppingCartItem(ShoppingCartItemUpdateCountVo vo) {
        LambdaUpdateWrapper<ShoppingCartItemEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ShoppingCartItemEntity::getMemberId, authTokenService.getMemberInfo().getMemberId())
                .eq(ShoppingCartItemEntity::getSkuId, vo.getSkuId())
                .set(ShoppingCartItemEntity::getPurchaseQuantity, vo.getCount());
        update(wrapper);
    }

    @Override
    @Transactional
    public void addShoppingCart(ShoppingCartVo shoppingCartVo) {
        ShoppingCartItemEntity entity = getOneBySkuId(shoppingCartVo.getSkuId());
        if (entity == null) {
            entity = new ShoppingCartItemEntity();
            entity.setMemberId(authTokenService.getMemberInfo().getMemberId());
            entity.setSkuId(shoppingCartVo.getSkuId());
            entity.setSelected(shoppingCartVo.getSelected());
            entity.setPurchaseQuantity(shoppingCartVo.getCount());
            save(entity);
        } else {
            long count = entity.getPurchaseQuantity() + shoppingCartVo.getCount();
            LambdaUpdateWrapper<ShoppingCartItemEntity> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(ShoppingCartItemEntity::getMemberId, authTokenService.getMemberInfo().getMemberId())
                    .eq(ShoppingCartItemEntity::getSkuId, shoppingCartVo.getSkuId())
                    .set(ShoppingCartItemEntity::getPurchaseQuantity, count);
            update(wrapper);
        }
    }

    @Override
    public List<ShoppingCartVo> getSelectedList(List<String> skuIds) {
        LambdaQueryWrapper<ShoppingCartItemEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCartItemEntity::getMemberId, authTokenService.getMemberInfo().getMemberId())
                .in(ShoppingCartItemEntity::getSkuId, skuIds);
        List<ShoppingCartVo> voList = new ArrayList<>();
        list(wrapper).forEach(item -> {
            ShoppingCartVo vo = new ShoppingCartVo();
            vo.setSelected(item.getSelected());
            vo.setSkuId(item.getSkuId());
            vo.setCount(item.getPurchaseQuantity());
            vo.setId(item.getId());
        });
        return voList;
    }

    @Override
    public PageUtils shoppingCartList(ShoppingCartQuery cartQuery) {
        LambdaQueryWrapper<ShoppingCartItemEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCartItemEntity::getMemberId, authTokenService.getMemberInfo().getMemberId())
                .orderByDesc(ShoppingCartItemEntity::getCreateTime);
        return new PageUtils(baseMapper.selectPage(new Page<>(cartQuery.getPageNum(), cartQuery.getPageSize()), wrapper));
    }
}